package com.vogue.supplymall.admin.business.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2018-05-31.
 * 下运行日志
 */
@Controller
public class DebugController extends BaseController {

    private String cmd1 = "/usr/local/supplyapps/script/ziplog.sh";
    private String path1 = "/usr/local/supplyapps/logs";
    private String cmd2 = "/usr/local/supplyapps/script/ziplog.sh";
    private String path2 = "/usr/local/supplyapps/logs";

    /**
     * @param moudle moudle
     * @param debug debug
     * @param req req
     * @param response response
     * @throws IOException IOException
     * @throws InterruptedException InterruptedException
     */
    @RequestMapping(value = "/is/{appver}/admin/business/downlog", method = RequestMethod.GET)
    public void downlog(@RequestParam String moudle, @RequestParam String debug, HttpServletRequest req,
                        HttpServletResponse response) throws IOException, InterruptedException {
        String cmd, path;
        if ("true".equals(debug)) {
            cmd = cmd2;
            path = path2;
        } else {
            cmd = cmd1;
            path = path1;
        }
        Process p = Runtime.getRuntime().exec(cmd + " " + moudle); //执行脚本压缩日志
        p.waitFor(); //等待压缩结束

        response.setContentType("text/html;charset=utf-8");
        java.io.BufferedInputStream bis = null;
        java.io.BufferedOutputStream bos = null;
        String f = path + "/" + moudle + ".tgz";
        try {
            long fileLength = new File(f).length();
            response.setContentType("application/x-msdownload;");
            response.setHeader("Content-Disposition", "attachment;fileName=" + moudle + ".tgz");
            response.setHeader("Content-Length", String.valueOf(fileLength));
            bis = new BufferedInputStream(new FileInputStream(f));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
